package com.fs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.mapper.*;
import com.fs.mapper.Inventory;
import com.fs.pojo.*;
import com.fs.service.OutstoService;
import com.fs.vo.Condition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author hyy
 * @version 1.0
 * @description: 销售出货审批申请服务层
 * @date 2024/10/18 18:58
 */
@Service
public class OutstoServiceImpl implements OutstoService {
    @Autowired
    private OutstoMapper outstoMapper;

    @Autowired
    private ReserveMapper reserveMapper;

    @Autowired
    private WhOutboundMapper whOutboundMapper;

    @Autowired
    private InventoryMapper inventoryMapper;

    @Autowired
    private WhDetailedMapper whDetailedMapper;

    @Override
    public Page<Outsto> queryAll(Condition<Outsto> condition) {
        Page<Outsto> page = new Page<>(condition.getPage(),condition.getCount());
        page.setDesc("outSto_id");
        //构建条件构造器
        LambdaQueryWrapper<Outsto> wrapper = new LambdaQueryWrapper<Outsto>();
        Outsto outsto=condition.getSearch();
        if (Objects.nonNull(outsto)){
            //供应商姓名模糊查询条件
            wrapper.eq(outsto.getOutStoState()!=null,Outsto::getOutStoState,outsto.getOutStoState())
                    .eq(outsto.getOutStoId()!=null,Outsto::getOutStoId,outsto.getOutStoId());
        }
        return outstoMapper.selectPage(page,wrapper);
    }

    @Override
    @Transactional
    public Boolean updateState(Outsto outsto) {
        if (outsto.getOutStoState()==1){
            outsto.setOutStoDate(new Date());
            int update = outstoMapper.updateById(outsto);
            if (update !=0) {
                //出库审批状态修改成功，通过，修改仓库库存
                Wh_inventory whInventory = reserveMapper.selectOne(new LambdaQueryWrapper<Wh_inventory>().eq(Wh_inventory::getProductId, outsto.getSaleOutinventory().get(0).getProductId())
                        .eq(Wh_inventory::getWarehouseId,outsto.getWarehouseId()));
                //修改库存数量___出库，数量减少
                whInventory.setWarehouseId(outsto.getWarehouseId());
                whInventory.setQuantity( whInventory.getQuantity()-outsto.getSaleOutinventory().get(0).getSalesDetNum());
                reserveMapper.updateById(whInventory);

                //修改销售库存——————销售库存添加
                inventoryMapper.updateNumById(outsto.getSaleOutinventory().get(0).getProductId(),(-1)*outsto.getSaleOutinventory().get(0).getSalesDetNum());
                //添加出库日志
                WhOutbound whOutbound = new WhOutbound(outsto.getOutStoActor(),outsto.getOutStoDate(),"销售出库", outsto.getWarehouseId(), outsto.getSaleOutinventory().get(0).getSalesDetNum(), outsto.getSaleOutinventory().get(0).getNote(), outsto.getOutStoState(), outsto.getUserId(), outsto.getOutStoDate());
                whOutboundMapper.insert(whOutbound);
                //插入对应的详情信息
                whDetailedMapper.insert(new WhDetailed(whOutbound.getOutboundEntryId(),outsto.getSaleOutinventory().get(0).getProductId(),outsto.getSaleOutinventory().get(0).getProductName(),outsto.getSaleOutinventory().get(0).getSalesDetNum()));
            }
            return update!=0?true:false;
        }else {
            outstoMapper.updateById(outsto);
            return false;
        }
    }

    @Override
    public Outsto queryById(Integer outStoId) {
        return outstoMapper.selectByOutStoId(outStoId);
    }

    /**
     * 添加出库审批表
     * @param outsto 出库审批表对象
     * @return 添加结果
     */
    @Transactional
    @Override
    public Integer addOutsto(Outsto outsto) {
        outstoMapper.addOutsto(outsto);
        Integer outStoId = outsto.getOutStoId();
        outsto.setStoId(outStoId);
        outstoMapper.updateById(outsto);
        int i = 0;
        for (SaleOutinventory saleOutinventory : outsto.getSaleOutinventory()) {
            saleOutinventory.setOrderId(outStoId);
            i = outstoMapper.addOutstoDetails(saleOutinventory);
        }
        return outStoId != null && i != 0 ? 1 : 0;
    }


    public Integer delOutsto(Integer id) {
        return outstoMapper.deleteById(id);
    }
}
